package com.zmx.patients.view.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.wang.adapters.interfaceabstract.OnItemClickListener;
import com.zmx.patients.BaseActivity;
import com.zmx.patients.R;
import com.zmx.patients.adapter.MallGoodsAdapter;
import com.zmx.patients.okhttp.OkHttp3ClientManager;
import com.zmx.patients.pojo.MallGoods;
import com.zmx.patients.utils.AllUtil;
import com.zmx.patients.view.util.MySharedPreferences;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class GoodsItemActivity extends BaseActivity {

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    private MallGoodsAdapter mgAdapter;
    private List<MallGoods> mallGoods;

    private String group_id;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_goods_item;
    }

    @Override
    protected void initViews() {
        ButterKnife.bind(this);
        setToolbar(R.id.tool_bar);
        group_id = this.getIntent().getStringExtra("id");

        mgAdapter = new MallGoodsAdapter(mActivity);
        recyclerView.setLayoutManager(new GridLayoutManager(mActivity, 3));
        recyclerView.setAdapter(mgAdapter);
        mallGoods = new ArrayList<>();
        mgAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            protected void onItemClick(View view, int listPosition) {

                Intent intent = new Intent(mActivity, GoodsDetailsActivity.class);
                intent.putExtra("goods_id", mallGoods.get(listPosition).getGoods_id()+"");
                startActivity(intent);
            }
        });

        getGroupGoods();

    }


    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler(){

        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);

            switch (msg.what){

                case 1:

                    try {

                        JSONObject object = new JSONObject(msg.obj.toString());
                        JSONArray data = object.getJSONArray("data");
                        Gson g = new Gson();

                        for (int i = 0;i<data.length();i++){

                            JSONObject jsonObject = data.getJSONObject(i);
                            MallGoods gc = g.fromJson(jsonObject.toString(),MallGoods.class);
                            mallGoods.add(gc);

                        }

                        mgAdapter.setListAndNotifyDataSetChanged(mallGoods);

                    } catch (JSONException e) {
                        Log.e("异常","异常"+e.toString());
                        e.printStackTrace();
                    }

                    break;

            }

        }
    };

    public void getGroupGoods(){

        List<String> ls = new ArrayList<>();
        ls.add(group_id);
        ls.add("13");
        ls.add("1");
        //请求网络
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("sign", AllUtil.md5(sing(ls)));
        params.put("token", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.token, ""));
        params.put("uid", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.uid, ""));
        params.put("ts", System.currentTimeMillis() / 1000); // 当前时间戳
        params.put("cmd", "getGroupGoods");
        params.put("data[group_id]", group_id);
        params.put("data[row]", 1 + "");
        params.put("data[number]", 13 + "");
        OkHttp3ClientManager.getInstance().NetworkRequestMode("http://zmx.123xk.cn/mall/", params, handler, 1, 404);

    }
}

